package com.leyou.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.PageResult;
import com.leyou.dao.*;
import com.leyou.pojo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class SpuService {
    @Autowired
    private SpuDetailMapper spuDetailMapper;
    @Autowired
    private SkuMapper skuMapper;

    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private StockMapper stockMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SpuMapper spuMapper;
    public PageResult<SpuVo> searchSpu(String key, Integer saleable, Integer page, Integer rows) {
        Example example = new Example(Spu.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(key)){
            criteria.andLike("title","%"+key+"%");
        }
        if (saleable!=2){
            criteria.andEqualTo("saleable",saleable);
        }
        //开始分页
        PageHelper.startPage(page,rows);
        //查询所有数据
        List<Spu>spus=spuMapper.selectByExample(example);
        //创建一个新的集合
        List<SpuVo> spuVos = new ArrayList<>();
        //根据获取的spu得到对应的分类
        for (Spu spu : spus) {
            SpuVo spuVo = new SpuVo();
            BeanUtils.copyProperties(spu,spuVo);
           List<String>cnames= categoryMapper.selectByIds(spuVo.getCid1(),spuVo.getCid2(),spuVo.getCid3());
            spuVo.setCname(StringUtils.join(cnames, "/"));
            //根据获取的spu得到对应的品牌name
            Brand brand = brandMapper.selectByPrimaryKey(spuVo.getBrandId());
            if (brand!=null){
                spuVo.setBname(brand.getName());
            }
            spuVos.add(spuVo);
        }
        PageInfo<SpuVo> pageInfo = new PageInfo<>(spuVos);
        PageResult<SpuVo> result = new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
       return result;
    }

    public void save(SpuVo spuVo) {
        //添加spu
        spuVo.setId(null);
        spuVo.setSaleable(true);
        spuVo.setValid(true);
        spuVo.setCreateTime(new Date());
        spuVo.setLastUpdateTime(spuVo.getCreateTime());
        this.spuMapper.insertSelective(spuVo);
        Integer id = spuVo.getId();
        //添加detail
        SpuDetail spu_detail = spuVo.getSpuDetail();
        spu_detail.setSpuId(id);
        spuDetailMapper.insert(spu_detail);
        //添加skus
        List<Sku> skus = spuVo.getSkus();
        for (Sku sku : skus) {
            sku.setSpuId(id);
            sku.setCreateTime(new Date());
            sku.setLastUpdateTime(sku.getCreateTime());
            this.skuMapper.insert(sku);
            //添加stock
            Stock stock = new Stock();
            stock.setSkuId(sku.getId());
            stock.setStock(sku.getStock());
            this.stockMapper.insertSelective(stock);
        }

    }

    public SpuDetail detail(Integer spu_id) {
        SpuDetail spuDetail= this.spuDetailMapper.detail(spu_id);
        return spuDetail;
    }

    public List<Sku> list(Integer spu_id) {
        List<Sku> list=this.skuMapper.list(spu_id);
      /*  for (Sku sku : list) {
            Stock stock = this.stockMapper.selectByPrimaryKey(sku.getId());
            sku.setStock(stock.getStock());
        }*/
        return list;
    }

    public void update(SpuVo spuVo) {
       //修改spu表
        spuVo.setCreateTime(new Date());
        spuVo.setLastUpdateTime(spuVo.getCreateTime());
        spuMapper.updateByPrimaryKey(spuVo);
        //修改detail表
        SpuDetail spuDetail = spuVo.getSpuDetail();
        spuDetail.setSpuId(spuVo.getId());
        spuDetailMapper.updateByPrimaryKey(spuDetail);
        //修改sku集合和stock
        List<Sku> skus = spuVo.getSkus();
        for (Sku sku : skus) {
            sku.setCreateTime(new Date());
            sku.setLastUpdateTime(sku.getCreateTime());
            skuMapper.updateByPrimaryKey(sku);
            //修改stock
            Integer skuId = sku.getId();
            stockMapper.update(skuId,sku.getStock());
        }
    }

    public void deleteMany(Integer id) {
        //先删除sku的stock
        //通过spu的id查询出所有的sku
        List<Sku>list=skuMapper.list(id);
        for (Sku sku : list) {
            Integer skuId = sku.getId();
            //根据skuid 删除所有的stock
            stockMapper.deleteByPrimaryKey(skuId);
            //删除所有的sku
            skuMapper.deleteByPrimaryKey(skuId);
        }

        //删除detail
        spuDetailMapper.deleteByPrimaryKey(id);
        //删除spu
        spuMapper.deleteByPrimaryKey(id);


    }

    public void upOrDown(Integer spuId, Integer saleable) {
        spuMapper.upOrDown(spuId,saleable);
        List<Sku> list = skuMapper.list(spuId);
        for (Sku sku : list) {
            skuMapper.upOrDown(sku.getId(),saleable);
        }
    }

    public Spu findSpuBuId(Integer spuid) {
        Spu spu=  spuMapper.findSpuBuId(spuid);
        return spu;
    }
}
